/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;

import Server.Server;

/**
 * 
 * @author TienNT
 */
public class Client_Login extends javax.swing.JFrame {
	Client mClient;

	/**
	 * Creates new form Login
	 */
	public Client_Login(Client client) {
		mClient = client;
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		txtUsername = new javax.swing.JTextField();
		btnLogin = new javax.swing.JButton();
		btnRegister = new javax.swing.JButton();
		jLabel4 = new javax.swing.JLabel();
		txtPassword = new javax.swing.JPasswordField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("Username: ");

		jLabel2.setText("Password");

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
		jLabel3.setText("JAVA LOCAL CHAT");

		btnLogin.setText("Login");
		btnLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnLoginActionPerformed(evt);
			}
		});

		btnRegister.setText("Register");
		btnRegister.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnRegisterActionPerformed(evt);
			}
		});

		jLabel4.setText("Don't have account? ");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(22, 22,
																		22)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addComponent(
																										jLabel1,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										69,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										txtUsername))
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addComponent(
																										jLabel2,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										68,
																										Short.MAX_VALUE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										txtPassword,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										191,
																										javax.swing.GroupLayout.PREFERRED_SIZE))))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(35, 35,
																		35)
																.addComponent(
																		jLabel4))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(128,
																		128,
																		128)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.CENTER)
																				.addComponent(
																						btnLogin,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						73,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						btnRegister)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(56, 56,
																		56)
																.addComponent(
																		jLabel3)))
								.addGap(45, 45, 45)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jLabel3,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										62,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(36, 36, 36)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jLabel1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														24,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														txtUsername,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														txtPassword,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										35, Short.MAX_VALUE)
								.addComponent(btnLogin).addGap(31, 31, 31)
								.addComponent(jLabel4).addGap(10, 10, 10)
								.addComponent(btnRegister).addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnLoginActionPerformed
		// TODO add your handling code here:
		String username = txtUsername.getText();
		mClient.setClientName(username);
		String password = new String(txtPassword.getPassword());
		DataInputStream dataInputStream = null;
		DataOutputStream dataOutputStream = null;
		try {
			dataInputStream = new DataInputStream(mClient.getSocket()
					.getInputStream());
			dataOutputStream = new DataOutputStream(mClient.getSocket()
					.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (dataInputStream != null && dataOutputStream != null) {
			try {
				dataOutputStream.writeUTF(ClientUtils.COMMAND_LOGIN + ":"
						+ username + ":" + password);
				dataOutputStream.flush();
				Thread.sleep(1000);
				int replyCode = dataInputStream.readInt();
				if (replyCode == ClientUtils.ACCEPT) {
					mClient.setStatus(ClientUtils.CONNECTED);
					System.out.println("Login" + Main.getContactList().size());
					setVisible(false);
				} else {
					// Bao loi sai username hoac password
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}// GEN-LAST:event_btnLoginActionPerformed

	private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnRegisterActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_btnRegisterActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btnLogin;
	private javax.swing.JButton btnRegister;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPasswordField txtPassword;
	private javax.swing.JTextField txtUsername;
	// End of variables declaration//GEN-END:variables
}
